import 'package:dio/dio.dart';
import 'package:flutterlib/common/config/config.dart';
import 'package:flutterlib/common/utils/utils.dart';

/**
 * Log 拦截器
 * Created by guoshuyu
 * on 2019/3/23.
 */
class LogsInterceptors extends InterceptorsWrapper {
  @override
  onRequest(RequestOptions options) async {
    trueDebugPrint("flutterhttp 请求url：${options.path}");
    trueDebugPrint("flutterhttp 请求头: " + options.headers.toString());
    if (options.data != null) {
      trueDebugPrint('flutterhttp 请求参数: ' + options.data.toString());
    }
    if (options.queryParameters != null) {
      options.queryParameters.forEach((key, value) {
        trueDebugPrint('flutterhttp queryparameters: ' +
            key.toString() +
            " " +
            value.toString());
      });
    }
    return options;
  }

  @override
  onResponse(Response response) async {
    //如果是文件流的话，要判断一下不要打印
    RequestOptions options = response.request;
    if (options.responseType == ResponseType.bytes ||
        options.responseType == ResponseType.stream) {
      return response;
    }
    if (response != null) {
      var responseOut = response.toString();
      if (response.toString().length > 100) {
        responseOut = responseOut.substring(0, 100);
      }
      trueDebugPrint("flutterhttp onresponse" + response.toString());
      // print('返回参数: ' + response.toString());
    }
    return response; // continue
  }

  @override
  onError(DioError err) async {
    trueDebugPrint('请求异常: ' + err.toString());
    trueDebugPrint('请求异常信息: ' + err.response?.toString() ?? "");
    return err; // continue;
  }
}
